<?php

namespace App\Models;

class AdminModel
{
    /**
     * 获取管理员信息
     * entry conn token
     * return array
     * */
    public static function getAdminInToken($conn,$token){
        $sql="SELECT `id`,`token`, `username`,`level`,`bid`,`money`,`withdraw_fees`, `time`,`last_time`,`last_ip`,`status`,`remarks` FROM `admin` WHERE `token` = '$token'";
        return $conn->readConn($sql);
    }
    /**
     * 登录获取管理员信息
     * entry conn $AdminExample
     * return array
     * */
    public static function getAdminInUsername($conn,$AdminExample){
        $adminSql="select `id`,`token`,`password`, `username`,`level`,`bid`,`money`,`withdraw_fees`, `time`,`last_time`,`last_ip`,`status`,`remarks` from `admin` where `username` = '$AdminExample->username'";
        return $conn->readConn($adminSql);
    }
    /**
     * 登录获取管理员所有菜单
     * entry conn $AdminExample
     * return array
     * */
    public static function getAdminAllMenu($conn,$AdminExample){
        $adminSql="SELECT `id`,`level`,`pid`,`path`, `component`, `redirect`, `name`, `title`, `icon` FROM `admin_menu` as a inner join `menu` as m on a.mid = m.id WHERE `aid` = '$AdminExample->id' order by `sort` desc";
        return $conn->readConn($adminSql,true);
    }
    /**
     * 登录获取管理员所有权限
     * entry conn $AdminExample
     * return array
     * */
    public static function getAdminNextMenu($conn,$pid,$AdminExample){
        $adminSql="SELECT `id`,`path`, `component`, `redirect`, `name`, `title`, `icon` FROM `admin_menu` as a inner join `menu` as m on a.mid = m.id WHERE `aid` = $AdminExample->id and `level` =2 and `pid` = $pid order by `sort` desc";
        return $conn->readConn($adminSql,true);
    }
    /**
     * 添加管理员日志
     * entry conn $AdminExample
     * return array
     * */
    public static function addAdminLog($conn,$assemble){
        $insetSql="INSERT INTO `admin_log`(`username`, `type`, `content`) VALUES ('".$assemble->username."','".$assemble->type."','".$assemble->content."')";
        return $conn->executeConn($insetSql);
    }
    /**
     * 修改管理员信息可添加字段
     * entry AdminExample
     * return int
     * */
    public static function upAdminInside($conn,$AdminExample){
        $joinSql="";
        if($AdminExample->username !=null){
            $joinSql.=" `username` = '$AdminExample->username',";
        }
        if($AdminExample->pid !=null){
            $joinSql.=" `pid` = '$AdminExample->pid',";
        }
        if($AdminExample->password !=null){
            $joinSql.=" `password` = '$AdminExample->password',";
        }
        if($AdminExample->last_ip !=null){
            $joinSql.=" `last_ip` = '$AdminExample->last_ip',";
        }
        if($AdminExample->last_time !=null){
            $joinSql.=" `last_time` = $AdminExample->last_time,";
        }
        if($AdminExample->status !=null){
            $joinSql.=" `status` = $AdminExample->status,";
        }
        if($AdminExample->level !=null){
            $joinSql.=" `level` = $AdminExample->level,";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `admin` SET $innerJoin WHERE `token` = '$AdminExample->token'";
            return $conn->executeConn($upUserSql);
        }
    }
}